package cruds.accessories.brand;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;

import utils.StaticMethods;
import home.Home;
import items.BrandDTO;
import cruds.CrudModifyParent;
import dynamicmenus.BrandModelMenu;
import exceptions.EmptyFieldException;

@SuppressWarnings("serial")
public class CrudModifyBrand extends CrudModifyParent<BrandDTO>{

	public CrudModifyBrand(Home home, BrandDTO item) {
		super(home, item);
	}

	@Override
	protected void redirectIfCancel(AjaxRequestTarget target) {
		BrandsList lista = new BrandsList(this.getHome(),this.getItem().getCategory(),this.getHome().getGeneralService().getBrandSer().getBrandsAndModels(this.getItem().getCategory()));
		lista.changePanel(target);
		
	}

	@Override
	protected void doUpdateAndRedirect(AjaxRequestTarget target) {
		try{
			StaticMethods.hasEmptyFields(this.getItem());
			this.getService().update(this.getItem());
			BrandsList lista = new BrandsList(this.getHome(),this.getItem().getCategory(),this.getHome().getGeneralService().getBrandSer().getBrandsAndModels(this.getItem().getCategory()));
			lista.changePanel(target);
		}catch(EmptyFieldException e){
			this.setErrorMessage(target, e.getMessage());
		}
		
	}

	@Override
	protected void updateDynamicMenu(AjaxRequestTarget target) {
		BrandModelMenu menu = new BrandModelMenu(this.getHome());
		menu.changePanel(target);
	}

	@Override
	protected void createFormFields(Form<BrandDTO> form) {
		form.add(new TextField<String>("brand"));
		form.add(new TextField<String>("model"));
	}

	@Override
	protected void initializeService() {
		this.setService(this.getHome().getGeneralService().getBrandSer());
	}

}
